// 限制ip访问次数
import Redis from 'ioredis'
import getMAC from 'getmac'
import { Models } from '../common/typings/model'

const security = async (ctx: Models.Ctx, next: Function) => {
  getMAC() //获取mac地址
  // const cache = new Redis({
  //     port: 6300, // Redis port
  //     host: "192.100.50.256", // Redis host
  //     password: "123"
  // });

  const cache = new Redis(6000)
  cache
    .get('eggsy')
    .then((data) => {
      console.log(15, data)
      if (parseInt(data || '0') > 5) {
        throw new Error('big than 5')
      }
      if (!data) {
        cache.set('eggsy', 1, 'EX', 60) //设置60秒内访问次数大于5次，报错
      } else {
        cache.incr('eggsy')
      }
    })
    .catch((err) => {
      console.log(err)
    })
  await next()
}
export default security
